package com.credit.mola.app.utils.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.credit.mola.app.R
import com.credit.mola.app.databinding.DialogStarActiveBinding
import com.credit.mola.app.pro.ProDialog
import com.credit.mola.app.utils.event.EventType
import com.credit.mola.app.utils.helper.StarDialogHelper
import com.credit.mola.app.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 打分评价dialog
 */
class StarActiveDialog: ProDialog {
    private lateinit var binding: DialogStarActiveBinding
    private var mCallback: StarDialogHelper.IStarDialogCallback? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: StarDialogHelper.IStarDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: StarDialogHelper.IStarDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.WRAP_CONTENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim)
        window?.setGravity(Gravity.CENTER)
    }

    override fun getRootView(): View {
        binding = DialogStarActiveBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.btnNotNow)
        setOnClickListener(R.id.btnRate)
        binding.ratingBar.setIsIndicator(false)
        binding.ratingBar.setOnRatingChangeListener { ratingBar, rating, fromUser ->
            HttpHelper.addEvent(EventType.CLICK.CLICK_USER_RATE_US_STAR,rating.toInt().toString())
        }
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        when(v?.id){
            R.id.ivClose,
            R.id.btnNotNow -> {//关闭、取消
                HttpHelper.addEvent(EventType.CLICK.CLICK_USER_RATE_US_NOT_NOW,"")
                dismiss()
            }
            else -> {//确定
                val count = binding.ratingBar.rating.toInt()
                if (count <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback?.completeScoringCount(count)
                }
                dismiss()
            }
        }
    }

    fun setTips(tips: String){
        binding.tvMsg.text = tips
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_RATE_US_SHOW,"Active Dialog")
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_USER_RATE_US_DISMISS,"Active Dialog")
    }

}